//
//  BFShSettingHomeViewController.m
//  BeautyFood
//
//  Created by apple on 2020/5/2.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THSettingHomeViewController.h"
#import "THAccountMangerViewController.h"
#import "THSuggestViewController.h"
#import "THAboutUsViewController.h"
@interface THSettingHomeViewController ()
@property(nonatomic, strong) NSMutableArray<NSArray*>                     *dataSouce;/**<  */
@end

@implementation THSettingHomeViewController

#pragma mark --vc生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    [self config_dataSouce];
    [self stoneobs_ui_configSubView];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
- (void)config_dataSouce{
    self.dataSouce = NSMutableArray.new;

    
    ({
        CGFloat size = [SDImageCache sharedImageCache].totalDiskSize / 1000.0 /1000;
       NSArray * array = @[
           [[STTools_TableEasyModel alloc] initWithTextString:@"账号管理" detailString:@""],
           [[STTools_TableEasyModel alloc] initWithTextString:@"意见反馈" detailString:@""],
           [[STTools_TableEasyModel alloc] initWithTextString:@"关于我们" detailString:@""],
           [[STTools_TableEasyModel alloc] initWithTextString:@"清除缓存" detailString:[NSString stringWithFormat:@"%0.2fM",size]],
       ];
        [self.dataSouce addObject:array];
    });
    
    [self.tableView reloadData];
}
#pragma mark --configSubView
- (void)stoneobs_ui_configSubView{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, STONEOBS_UI_UISCREEN_WIDTH, 100)];
    
    STButton * button = [[STButton alloc] initWithFrame:CGRectMake(33, 0, STONEOBS_UI_UISCREEN_WIDTH - 66, 44)
                                                  title:@"退出登录"
                                             titleColor:UIColor.whiteColor
                                              titleFont:15
                                           cornerRadius:18
                                        backgroundColor:STONEOBS_UI_THEME_BACKGROUNDCOLOR
                                        backgroundImage:nil
                                                  image:nil];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    button.centerX = self.tableView.width / 2;
    button.centerY = footer.height /2;
    [footer addSubview:button];
    [button setClicAction:^(UIButton *sender) {
        [weakSelf stoneobs_action_on_slected_log_out];
    }];
    self.tableView.tableFooterView = footer;
}
#pragma mark --stoneobs Action Method
- (void)stoneobs_action_on_slected_log_out{
    [self stoneobs_fun_st_showAlertTitle:@"确定要退出登录吗？" message:@"" leftTitle:@"取消" rightTitle:@"退出" block:^(NSString *name) {
        if ([name isEqualToString:@"退出"]) {
            [STTools_UserManger.defult stoneobs_fun_removeUserPreferce];
            UIWindow * window = UIApplication.sharedApplication.delegate.window;
            window.rootViewController = [[RTRootNavigationController alloc] initWithRootViewController:THLoginHomeViewController.new];
            [window stoneobs_fun_st_showAnimationWithType:STAnimationTypecube_stoneobs_enum];

        }
    }];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce[section].count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    STTools_TableEasyModel * model = self.dataSouce[indexPath.section][indexPath.row];
    [cell setSt_tableEasyModel:model];
    if (model.detailString.length > 0) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }else{
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    STTools_TableEasyModel * model = self.dataSouce[indexPath.section][indexPath.row];
    if ([model.textString isEqualToString:@"账号管理"]) {
        [self pushViewController:THAccountMangerViewController.new animated:1];
    }
    if ([model.textString isEqualToString:@"意见反馈"]) {
        [self pushViewController:THSuggestViewController.new animated:1];
    }
    if ([model.textString isEqualToString:@"关于我们"]) {
        [self pushViewController:THAboutUsViewController.new animated:1];
    }
    
    if ([model.textString containsString:@"清除缓存"]) {
        [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
            model.detailString = @"0.00M";
            [self.tableView reloadData];
            [SVProgressHUD showSuccessWithStatus:@"清除成功"];
        }];
        
    }

    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

@end
